/*
Copyright © 2025 NAME HERE <EMAIL ADDRESS>
*/
package cmd

import (
	"fmt"
	"io/ioutil"
	"net/http"

	"github.com/spf13/cobra"
)

// healthCmd represents the health command
var service_healthCmd = &cobra.Command{
	Use: "health",
	Run: func(cmd *cobra.Command, args []string) {
		client := http.Client{}
		req, err := http.NewRequest("GET", cmd_url_prefix+"/api/service/health", nil)
		if err != nil {
			fmt.Printf("创建请求失败: %v\n", err)
			return
		}

		q := req.URL.Query()
		name, _ := cmd.Flags().GetString("name")
		node, _ := cmd.Flags().GetString("node")
		q.Add("name", name)
		q.Add("node", node)
		req.URL.RawQuery = q.Encode()

		resp, err := client.Do(req)
		if err != nil {
			fmt.Printf("请求失败: %v\n", err)
			return
		}
		defer resp.Body.Close()
		buf, _ := ioutil.ReadAll(resp.Body)
		fmt.Print(string(buf))
	},
}

func init() {
	serviceCmd.AddCommand(service_healthCmd)
	service_healthCmd.Flags().String("name", "", "service name")
	service_healthCmd.Flags().String("node", "", "node name")

	// service_healthCmd.MarkFlagRequired("name")
}
